package org.onvivals.module.system.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import org.onvivals.common.param.ExistenceParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.system.entity.SysUser;
import org.onvivals.module.system.param.SysUserParam;
import org.onvivals.module.system.service.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/system/user")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysUserController {

    private final ISysUserService sysUserService;

    /**
     * 分页查询用户
     * @param param
     * @return
     */
    @Operation(summary = "分页查询用户")
    @GetMapping("/page")
    public Result<?> page(SysUserParam param) {
        PageResult<SysUser> pageResult = sysUserService.pageRel(param);
        return Result.OK("查询成功",pageResult);
    }

    /**
     * 新增或修改用户
     */
    @Operation(summary = "新增或修改用户")
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.PUT})
    public Result<?> add(@RequestBody SysUser sysUser) {
        sysUserService.saveOrUpdateUser(sysUser);
        return Result.OK("操作成功");
    }

    @Operation(summary = "重置密码")
    @PutMapping("/password")
    public Result<?> resetPassword(@RequestBody SysUser user) {
        if (user.getUserId() == null || StrUtil.isBlank(user.getPassword())) {
            return Result.error("参数不正确");
        }
        SysUser u = new SysUser();
        u.setUserId(user.getUserId());
        //2. 校验用户名或密码是否正确
        String salt = RandomUtil.randomString(10);
        String userPassword = SaSecureUtil.md5BySalt(user.getPassword(), salt);
        user.setPassword(userPassword);
        if (sysUserService.updateById(u)) {
            return Result.OK("重置成功");
        } else {
            return Result.error("重置失败");
        }
    }

    @Operation(summary ="修改用户状态")
    @PutMapping("/status")
    public Result<?> updateStatus(@RequestBody SysUser user) {
        if (user.getUserId() == null || user.getStatus() == null || !Arrays.asList(0, 1).contains(user.getStatus())) {
            return Result.error("参数不正确");
        }
        SysUser u = new SysUser();
        u.setUserId(user.getUserId());
        u.setStatus(user.getStatus());
        if (sysUserService.updateById(u)) {
            return Result.OK("修改成功");
        }
        return Result.error("修改失败");
    }

    @Operation(summary ="检查用户是否存在")
    @GetMapping("/existence")
    public Result<?> existence(ExistenceParam<SysUser> param) {
        if (param.isExistence(sysUserService, SysUser::getUserId)) {
            return Result.OK(param.getValue() + "已存在");
        }
        return Result.error(param.getValue() + "不存在");
    }

    /**
     * 删除用户
     */
    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable("id") Long id) {
        if (sysUserService.removeById(id)) {
            return Result.OK("删除成功");
        }
        return Result.error("删除失败");
    }

}
